<template>
  <div class="upload">
    <el-upload
      :accept="accept"
      :auto-upload="false"
      :file-list="fileList"
      :multiple="false"
      :on-change="handleChange"
      :on-exceed="handleExceed"
      :on-remove="handleRemove"
      :show-file-list="false"
    >
      <slot name="uploadLink">
        <el-button :icon="Upload" size="small" type="primary">导入</el-button>
      </slot>
    </el-upload>
  </div>
</template>

<script>
import { uploadUrl } from "@/api/mains";
import { ElLoading } from "element-plus";
import { Upload } from "@element-plus/icons-vue";
export default defineComponent({
  name: "UploadDirect",
  components: {},
  props: {
    url: {
      type: String,
      default: "",
      required: true
    },
    accept: {
      type: String,
      default: ""
    },
    size: {
      type: Number,
      default: 1024,
      required: true
    }
  },
  emits: {
    callbackupload: null //fetchData事件没有检验
  },
  setup(props, { emit }) {
    const { proxy } = getCurrentInstance();
    const state = reactive({
      loading: false,
      title: "上传",
      fileList: [],
      url: "",
      accept: "",
      size: 0
    });

    state.accept = ref(props.accept);
    state.url = ref(props.url);
    state.size = ref(props.size);

    const handleChange = file => {
      if (file) {
        if (file.size > state.size) {
          proxy.$baseMessage(`文件大于${state.size}B`, "warning");
        } else {
          state.fileList.push(file);
          uploadFile();
        }
      }
    };

    const uploadFile = async () => {
      state.loading = true;
      if (state.fileList.length === 0) {
        proxy.$baseMessage("请选取文件", "warning");
        state.loading = false;
        return;
      } else {
        const loading = ElLoading.service({
          lock: true,
          text: "导入中，请稍候...",
          background: "rgba(0, 0, 0, 0.7)"
        });
        const formdata = new FormData();
        state.fileList.forEach(file => {
          console.info(file.raw);
          formdata.append("upfile", file.raw);
        });
        const responseData = await uploadUrl(props.url, formdata);
        const responseObj = JSON.parse(JSON.stringify(responseData));
        if (responseObj.code == 200) {
          proxy.$baseMessage(responseObj.msg, "success", false, "vab-hey-message-success");
        } else {
          proxy.$baseMessage(responseObj.msg, "error", false, "vab-hey-message-error");
        }
        state.fileList = [];
        emit("callbackupload", responseObj);
        state.loading = false;
        loading.close();
      }
    };

    const handleExceed = files => {
      proxy.$baseMessage(
        `当前限制选择1个文件，本次选择了
                ${files.length}
                个文件`,
        "error",
        "vab-hey-message-error"
      );
    };

    const handleRemove = (file, fileList) => {
      state.fileList = fileList;
    };

    return {
      ...toRefs(state),
      uploadFile,
      handleChange,
      handleExceed,
      handleRemove,
      Upload
    };
  }
});
</script>

<style lang="scss" scoped>
.upload {
  /* height: 45vh; */
  .upload-content {
    .el-upload__tip {
      display: block;
      height: 30px;
      line-height: 30px;
    }
    :deep() {
      .el-upload--picture-card {
        width: 128px;
        height: 128px;
        margin: 3px 8px 8px;
        border: 2px dashed #c0ccda;
      }
      .el-upload-list--picture {
        margin-bottom: 20px;
      }
      .el-upload-list--picture-card {
        .el-upload-list__item {
          width: 128px;
          height: 128px;
          margin: 3px 8px 8px;
        }
      }
    }
  }
}
.text-left {
  padding-left: 30px;
  text-align: left;
}
h3 {
  padding-bottom: 20px;
}
.cust_btn30 {
  padding-top: 30px;
  padding-bottom: 60px;
}
.text_fise {
  font-size: 16px;
}
.text_fise > div {
  padding-bottom: 20px;
}
</style>
